﻿namespace SharpF2.Model
{
	using System.Collections.Generic;
	using System.IO;
	using ModelView;

	/// <summary>
	/// Defines the UndoManagerLogExtractor type.
	/// </summary>
	public class UndoManagerLogExtractor
	{
		#region Public Members

		/// <summary>
		/// Retreive input and output file path from a log file
		/// </summary>
		/// <param name="logFilePath">The log file info</param>
		/// <returns>
		/// The list of input and output file path from a log file
		/// </returns>
		public static List<UndoFileItem> GetUndoFileItem(FileInfo logFilePath)
		{
			var undoFileItem = new List<UndoFileItem>();

			if (null == logFilePath)
				return undoFileItem;

			var fileStream = new StreamReader(logFilePath.OpenRead());
			var line = fileStream.ReadLine();
			var idOldFilePath = "Succeed to move file from ";
			while (line != null)
			{
				// If rename file succeed
				if (line.Contains(idOldFilePath))
				{
					var oldFileStart = line.IndexOf(idOldFilePath) + idOldFilePath.Length;
					var newFileStart = line.IndexOf(":", oldFileStart + 4) - 1;
					if (oldFileStart > 0 && newFileStart > oldFileStart)
					{
						var oldFile = line.Substring(oldFileStart, newFileStart - oldFileStart - " to ".Length);
						var newFile = line.Substring(newFileStart);
						undoFileItem.Add(new UndoFileItem(newFile, oldFile));
					}
				}

				line = fileStream.ReadLine();
			}

			return undoFileItem;
		}

		#endregion
	}
}